package web.servlet;

import domain.Account;
import domain.Log;
import domain.Product;
import service.LogService;
import service.ProductService;
import web.filter.AuthorityFilter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ProductFormServlet", urlPatterns = {"/ProductForm"})
public class ProductFormServlet extends HttpServlet {
    private static final String PRODUCT_FORM = "WEB-INF/jsp/catalog/Product.jsp";
    private ProductService productService = new ProductService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Product product = productService.getProductById(req.getParameter("productId"));
        product.setImageSize("big");
        Account loginUser = (Account) req.getSession().getAttribute("loginUser");
        if(loginUser != null){
            new LogService().newLog(new Log(loginUser.getUserid(), "查看了-" + product.getBreed() + "-宠物"));
        }
        req.getSession().setAttribute("showingProduct", product);
        req.getRequestDispatcher(PRODUCT_FORM).forward(req,resp);
    }
}
